home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / platformcfg.py < prev    next >
Encoding:
Text File  |  2007-11-12  |  6.5 KB  |  199 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. import os
  19. import util
  20. import config
  21. import _winreg
  22. import cPickle
  23. import string
  24. import prefs
  25. import tempfile
  26. import ctypes
  27. import resources
  28.  
  29. import proxyfind
  30.  
  31. proxy_info = proxyfind.get_proxy_info()
  32.  
  33. _specialFolderCSIDLs = {
  34.     'AppData': 0x001a,
  35.     "My Music": 0x000d,
  36.     "My Pictures": 0x0027,
  37.     "My Videos": 0x000e,
  38.     "My Documents": 0x0005,
  39.     "Desktop": 0x0000,
  40.     "Common AppData": 0x0023,
  41. }
  42.  
  43. def getSpecialFolder(name):
  44.     """Get the location of a special folder.  name should be one of the
  45.     following: 'AppData', 'My Music', 'My Pictures', 'My Videos', 
  46.     'My Documents', 'Desktop'.
  47.  
  48.     The path to the folder will be returned, or None if the lookup fails
  49.  
  50.     """
  51.  
  52.     buf = ctypes.create_unicode_buffer(260)
  53.     buf2 = ctypes.create_unicode_buffer(1024) 
  54.     SHGetSpecialFolderPath = ctypes.windll.shell32.SHGetSpecialFolderPathW
  55.     GetShortPathName = ctypes.windll.kernel32.GetShortPathNameW
  56.     csidl = _specialFolderCSIDLs[name]
  57.     if SHGetSpecialFolderPath(None, buf, csidl, False):
  58.         if GetShortPathName(buf, buf2, 1024):
  59.             return buf2.value
  60.         else:
  61.             return buf.value
  62.     else:
  63.         return None
  64.  
  65. _appDataDirectory = getSpecialFolder('AppData')
  66. _commonAppDataDirectory = getSpecialFolder("Common AppData")
  67. _baseMoviesDirectory = getSpecialFolder('My Videos')
  68. _nonVideoDirectory = getSpecialFolder('Desktop')
  69.  
  70. # The "My Videos" folder isn't guaranteed to be listed. If it isn't
  71. # there, we do this hack.
  72. if _baseMoviesDirectory is None:
  73.     _baseMoviesDirectory = os.path.join(getSpecialFolder('My Documents'),'My Videos')
  74.  
  75. def _getMoviesDirectory():
  76.     path = os.path.join(_baseMoviesDirectory, config.get(prefs.SHORT_APP_NAME))
  77.     try:
  78.         os.makedirs(os.path.join(path, 'Incomplete Downloads'))
  79.     except:
  80.         pass
  81.     return path
  82.  
  83. def _getSupportDirectory():
  84.     # We don't get the publisher and long app name from the config so
  85.     # changing the app name doesn't change the support directory
  86.     path = os.path.join(_appDataDirectory,
  87.                         u'Participatory Culture Foundation',
  88.                         u'Miro',
  89.                         u'Support')
  90.     try:
  91.         os.makedirs(path)
  92.     except:
  93.         pass
  94.     return path
  95.  
  96. def _getThemeDirectory():
  97.     # We don't get the publisher and long app name from the config so
  98.     # changing the app name doesn't change the support directory
  99.     path = os.path.join(_commonAppDataDirectory,
  100.                         u'Participatory Culture Foundation',
  101.                         u'Miro',
  102.                         u'Themes')
  103.     try:
  104.         os.makedirs(path)
  105.     except:
  106.         pass
  107.     return path
  108.  
  109. def _getConfigFile():
  110.     return os.path.join(_getSupportDirectory(), "preferences.bin")
  111.  
  112. def load():
  113.     try:
  114.         file = _getConfigFile()
  115.         if os.path.exists(file):
  116.             return cPickle.load(open(file))
  117.         else:
  118.             return {}
  119.     except:
  120.         import traceback
  121.         print "Error loading perferences. Resetting prefs."
  122.         traceback.print_exc()
  123.         return {}
  124.  
  125. def save(data):
  126.     file = _getConfigFile()
  127.     cPickle.dump(data,open(file,'w'))
  128.  
  129. def get(descriptor):
  130.     if descriptor == prefs.MOVIES_DIRECTORY:
  131.         return _getMoviesDirectory()
  132.  
  133.     elif descriptor == prefs.THEME_DIRECTORY:
  134.         return _getThemeDirectory()
  135.  
  136.     elif descriptor == prefs.NON_VIDEO_DIRECTORY:
  137.         return _nonVideoDirectory
  138.  
  139.     elif descriptor == prefs.GETTEXT_PATHNAME:
  140.         return resources.path("locale")
  141.  
  142.     elif descriptor == prefs.SUPPORT_DIRECTORY:
  143.         return _getSupportDirectory()
  144.  
  145.     elif descriptor == prefs.ICON_CACHE_DIRECTORY:
  146.         return os.path.join(_getSupportDirectory(), 'icon-cache')
  147.     
  148.     elif descriptor == prefs.DB_PATHNAME:
  149.         path = get(prefs.SUPPORT_DIRECTORY)
  150.         return os.path.join(path, 'tvdump')
  151.  
  152.     elif descriptor == prefs.BSDDB_PATHNAME:
  153.         path = get(prefs.SUPPORT_DIRECTORY)
  154.         return os.path.join(path, 'database')
  155.  
  156.     elif descriptor == prefs.SQLITE_PATHNAME:
  157.         path = get(prefs.SUPPORT_DIRECTORY)
  158.         return os.path.join(path, 'sqlitedb')
  159.  
  160.     elif descriptor == prefs.LOG_PATHNAME:
  161.         return os.path.join(tempfile.gettempdir(), ('%s.log' %config.get(prefs.SHORT_APP_NAME)))
  162.  
  163.     elif descriptor == prefs.DOWNLOADER_LOG_PATHNAME:
  164.         return os.path.join(tempfile.gettempdir(), ('%s-downloader.log'%config.get(prefs.SHORT_APP_NAME)))
  165.  
  166.     elif descriptor == prefs.RUN_AT_STARTUP:
  167.         # We use the legacy startup registry key, so legacy versions
  168.         # of Windows have a chance
  169.         # http://support.microsoft.com/?kbid=270035
  170.  
  171.         folder = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run")
  172.         count = 0
  173.         while True:
  174.             try:
  175.                 (name, val, type) = _winreg.EnumValue(folder,count)
  176.                 count += 1
  177.                 if (name == config.get(prefs.LONG_APP_NAME)):
  178.                     return True                    
  179.             except:
  180.                 return False
  181.         return False
  182.  
  183.     elif descriptor == prefs.HTTP_PROXY_ACTIVE:
  184.         return proxy_info.host is not None
  185.     elif descriptor == prefs.HTTP_PROXY_HOST:
  186.         return proxy_info.host
  187.     elif descriptor == prefs.HTTP_PROXY_PORT:
  188.         return proxy_info.port
  189.     elif descriptor == prefs.HTTP_PROXY_IGNORE_HOSTS:
  190.         return poxy_info.ignore_hosts
  191.     # Proxy authorization isn't suppored on windows, so the following keps are
  192.     # ignored:
  193.     # 
  194.     # HTTP_PROXY_AUTHORIZATION_ACTIVE
  195.     # HTTP_PROXY_AUTHORIZATION_USERNAME
  196.     # HTTP_PROXY_AUTHORIZATION_PASSWORD
  197.     else:
  198.         return descriptor.default
  199.